package com.dhemery.mack.model;

import java.util.ArrayList;
import java.util.List;

//TODO Move event manager stuff into separate class.
public class Folder extends Node {
	public static final String DEFAULT_FOLDER_NAME = "New Folder";
	private static List<FolderChangeListener> childChangeListeners = new ArrayList<FolderChangeListener>();
	
	public static void subscribeToChildChange(FolderChangeListener newSubscriber) {
		if (!childChangeListeners.contains(newSubscriber)) childChangeListeners.add(newSubscriber);
	}

	public static void unsubscribeFromChildChange(FolderChangeListener oldSubscriber) {
		childChangeListeners.remove(oldSubscriber);
	}

	private List<Node> nodes = new ArrayList<Node>();

	public Folder() {
		this(DEFAULT_FOLDER_NAME);
	}

	public Folder(String name) {
		super(name);
	}
	
	public void addNode(Node node) {
		nodes.add(node);
		node.setFolder(this);
		publishChildAdded(node);
	}

	public List<Node> getNodes() {
		return nodes;
	}

	private void publishChildAdded(Node node) {
		for(FolderChangeListener listener : childChangeListeners)
			listener.nodeAdded(this, node);
	}
}